layui.use(['layer', 'table', 'form', 'laydate'], function(){
    var layer = layui.layer //弹层
        , table = layui.table //表格
        , form = layui.form
        , laydate = layui.laydate;

    var resultTable = table.render({
        elem: '#demo'
        ,height: 500
        ,url: '/result/selectAll' //数据接口
        ,page: true //开启分页
        ,toolbar: '#navBar'
        ,limit:10
        ,limits:[5,10,15,20,25,30,50,100]
        ,autoSort:false
        ,cols: [
            [ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'testerId', title: 'ID', width:80, sort: true, fixed: 'left', templet: '<div>{{d.tester.testerId}}</div>'}
                ,{field: 'testerName', title: '用户名', templet: '<div>{{d.tester.testerName}}</div>'}
                ,{field: 'testerPhone', title: '手机号', templet: '<div>{{d.tester.testerPhone}}</div>'}
                ,{field: 'testTime', title: '测试时间', templet: '<div>{{d.tester.testTime}}</div>'}
                ,{field: 'color', title: '性格颜色', templet: '<div>{{d.character.hue}}</div>'}
                ,{field: 'red', title: '红色选项', templet: '<div>{{d.anum1 + d.dnum2}}</div>'}
                ,{field: 'yellow', title: '黄色选项', templet: '<div>{{d.bnum1 + d.cnum2}}</div>'}
                ,{field: 'blue', title: '蓝色选项', templet: '<div>{{d.cnum1 + d.bnum2}}</div>'}
                ,{field: 'green', title: '绿色选项', templet: '<div>{{d.dnum1 + d.anum2}}</div>'}
                ,{fixed: 'right',title: '操作', align: 'center', toolbar: '#barDemo'}
            ]
        ]
    });
    form.render('select', 'formDemo');
    table.on('toolbar(test)', function (obj) {
        switch (obj.event){
            case 'del':
                layer.confirm('真的删除么', function (index) {
                    var checkStatus = table.checkStatus(obj.config.id);
                    if(checkStatus.data.length != 0){

                        let arr = [];
                        for (let i=0;i<checkStatus.data.length;i++){
                            arr.push(checkStatus.data[i].tester.testerId);
                        }
                        $.ajax({
                            url:"/result/delete",
                            data: {id: arr.join(',')},
                            type:"delete",
                            success:function (result) {
                                //成功后的回调函数
                                if (result.code == 0) {
                                    layer.msg('删除成功');
                                }else {
                                    layer.msg('删除失败');
                                }
                                layer.close(index);
                                table.reload('demo', {
                                    page: {
                                        curr: resultTable.config.page.curr
                                    }
                                })
                            }
                        });
                    }

                });
                break;
        }
    });

    //删除
    table.on('tool(test)', function (obj) {
        var data = obj.data, layEvent = obj.event;
        if (layEvent === 'del') {
            layer.confirm('真的删除么', function (index) {
                $.ajax({
                    url:"/result/delete",
                    data: {id: data.tester.testerId},
                    type:"delete",
                    success:function (result) {
                        //成功后的回调函数
                        if (result.code == 0) {
                            obj.del(); //删除对应行（tr）的DOM结构
                            layer.msg('删除成功');
                        }else {
                            layer.msg('删除失败');
                        }
                        layer.close(index);
                        table.reload('demo', {
                            page: {
                                curr: resultTable.config.page.curr
                            }
                        })
                    }
                });
            })
        }
    })

});